package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.mapper.QuarterMapper;
import com.xmy.cultivate.service.IQuarterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 每年季度 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class QuarterServiceImpl extends ServiceImpl<QuarterMapper, Quarter> implements IQuarterService {

    @Autowired
    QuarterMapper quarterMapper;

    @Override
    public Quarter getNowQuarter(){
        LocalDate nowDate = LocalDate.now();
        QueryWrapper<Quarter> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("start_date",nowDate);
        queryWrapper.ge("end_date",nowDate);
        queryWrapper.last("limit 1");
        Quarter quarter = this.getOne(queryWrapper);
        return quarter;
    }

    @Override
    public Quarter getQuarterForYearAndNum(Integer yearPart,Integer quarterNum){
        QueryWrapper<Quarter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part",yearPart);
        queryWrapper.eq("num",quarterNum);
        queryWrapper.last("limit 1");
        Quarter quarter = this.getOne(queryWrapper);
        return quarter;
    }

    @Override
    public Quarter getLastForTranscription(Integer yearPart, Integer quarterNum){
        Quarter quarter = this.getLastQuarter(yearPart,quarterNum);
        if (quarter!=null){
            if (quarter.getType().equals(2)){
                quarter = this.getLastQuarter(quarter.getYearPart(),quarter.getNum());
            }
        }
        return quarter;
    }

    /**
     * 获取上一个季度
     * @return
     */
    public Quarter getLastQuarter(Integer yearPart,Integer quarterNum){
        return quarterMapper.getLastQuarter(yearPart,quarterNum);
    }

    /**
     * 获取前面第几个学期
     * @param yearPart
     * @param quarterNum
     * @param upNum
     * @return
     */
    @Override
    public Quarter getUpQuarter(Integer yearPart, Integer quarterNum, Integer upNum){
        Quarter quarter = this.getOneForYearAndNum(yearPart,quarterNum);
        QueryWrapper<Quarter> queryWrapper = new QueryWrapper<>();
        Integer upNumId = Integer.parseInt(quarter.getId().toString()) - upNum;
        if (upNumId<1){
            upNumId = 1;
        }
        queryWrapper.eq("id",upNumId);
        Quarter upQuarter = this.getOne(queryWrapper);
        upQuarter.setAliasName(upQuarter.getName());
        //upQuarter.setSort(upNum);
        return upQuarter;

    }

    /**
     * 根据年份与学期获取一条数据
     * @param yearPart
     * @param quarterNum
     * @return
     */
    @Override
    public Quarter getOneForYearAndNum(Integer yearPart,Integer quarterNum){
        QueryWrapper<Quarter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part",yearPart);
        queryWrapper.eq("num",quarterNum);
        queryWrapper.last("limit 1");
        Quarter quarter = this.getOne(queryWrapper);
        return quarter;
    }
}
